package com.zt.chatgpt.config.SenstitiveWord;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.utils.InnerWordCharUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: zt
 * @Description: 敏感词配置类
 * @create: 2024-05-21 22:30
 */
@Slf4j
@Configuration
public class SensitiveWordConfig {

    @Bean
    public SensitiveWordBs sensitiveWordBs() {
        return SensitiveWordBs.newInstance()
                // 默认是把敏感词删除
                .wordReplace((stringBuilder, chars, wordResult, iWordContext) -> {
                    // 敏感词
                    String sensitiveWord = InnerWordCharUtils.getString(chars, wordResult);
                    log.info("检测到敏感词：{}", sensitiveWord);
                    // 这里可以替换为,其实不用替换，gpt会处理
                    int wordLength = wordResult.endIndex() - wordResult.startIndex();
                    for (int i = 0; i < wordLength; i++) {
                        stringBuilder.append(sensitiveWord.charAt(i));
                    }

                })
                // 启用忽略大小写、宽度、数字样式、中文样式、英文样式选项
                .ignoreCase(true)
                .ignoreWidth(true)
                .ignoreNumStyle(true)
                .ignoreChineseStyle(true)
                .ignoreEnglishStyle(true)
                // 禁止忽略重复的敏感词
                .ignoreRepeat(false)
                // 开启邮箱、网址、数字检查
                .enableNumCheck(true)
                .enableEmailCheck(true)
                .enableUrlCheck(true)
                .enableWordCheck(true)
                // 设置数字检查的长度限制为1024
                .numCheckLen(1024)
                // 初始化并返回配置好的敏感词处理对象
                .init();
    }
}
